package com.battle.service.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.battle.pojo.entity.MotionEvaluation;
import com.battle.pojo.param.motionEvaluation.*;
import com.battle.pojo.vo.motionEvaluation.*;
import com.battle.pojo.dto.motionEvaluation.*;
import com.battle.service.repository.in.MotionEvaluationRepository;
import com.battle.service.service.in.MotionEvaluationService;
import nirvana.core.domains.vo.PageResult;
import nirvana.core.utils.ids.IdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;

/**
 * 【描 述】：运动评估参数服务
 * 【环 境】：J2SE 17
 * 【详 细】：
 */
@Service
public class MotionEvaluationServiceImpl implements MotionEvaluationService {

    private final MotionEvaluationRepository motionEvaluationRepository;

    public MotionEvaluationServiceImpl(MotionEvaluationRepository motionEvaluationRepository) {
        this.motionEvaluationRepository = motionEvaluationRepository;
    }


    /**
     * 创建运动评估参数
     *
     * @param param
     */
    @Override
    public void createMotionEvaluation(CreateMotionEvaluationParam param) {
        MotionEvaluation motionEvaluation = new MotionEvaluation();
        BeanUtils.copyProperties(param, motionEvaluation);
        Long id = IdGenerator.SNOW.generate();
        motionEvaluation.setId(id);

        if (param.getEndurance() != null) {
            motionEvaluation.setEndurance(JSONArray.toJSONString(param.getEndurance()));
        }
        if (param.getStrength() != null) {
            motionEvaluation.setStrength(JSONArray.toJSONString(param.getStrength()));
        }
        if (param.getSpeed() != null) {
            motionEvaluation.setSpeed(JSONArray.toJSONString(param.getSpeed()));
        }
        if (param.getAgility() != null) {
            motionEvaluation.setAgility(JSONArray.toJSONString(param.getAgility()));
        }
        if (param.getFlexibility() != null) {
            motionEvaluation.setFlexibility(JSONArray.toJSONString(param.getFlexibility()));
        }
        motionEvaluationRepository.create(motionEvaluation);
    }

    /**
     * 修改运动评估参数
     *
     * @param param
     */
    @Override
    public void editMotionEvaluation(EditMotionEvaluationParam param) {

        MotionEvaluation motionEvaluation = motionEvaluationRepository.getByEntity(new MotionEvaluation());
        if (motionEvaluation == null) {
            createMotionEvaluation(new CreateMotionEvaluationParam());
        }else{
            BeanUtils.copyProperties(param, motionEvaluation);
            if (param.getEndurance() != null) {
                motionEvaluation.setEndurance(JSONArray.toJSONString(param.getEndurance()));
            }
            if (param.getStrength() != null) {
                motionEvaluation.setStrength(JSONArray.toJSONString(param.getStrength()));
            }
            if (param.getSpeed() != null) {
                motionEvaluation.setSpeed(JSONArray.toJSONString(param.getSpeed()));
            }
            if (param.getAgility() != null) {
                motionEvaluation.setAgility(JSONArray.toJSONString(param.getAgility()));
            }
            if (param.getFlexibility() != null) {
                motionEvaluation.setFlexibility(JSONArray.toJSONString(param.getFlexibility()));
            }
            motionEvaluationRepository.edit(motionEvaluation);
        }
    }

    /**
     * 移除运动评估参数
     *
     * @param id
     */
    @Override
    public void removeMotionEvaluation(Long id) {
        motionEvaluationRepository.removeById(id);
    }

    /**
     * 启动/禁用运动评估参数
     *
     * @param id
     */
    @Override
    public void enabledMotionEvaluation(Long id) {
        MotionEvaluation motionEvaluation = motionEvaluationRepository.getByIdCheck(id);
        motionEvaluation.setIsDisable(!motionEvaluation.getIsDisable());
        motionEvaluationRepository.edit(motionEvaluation);
    }

    /**
     * 获取运动评估参数详情
     *
     * @param
     * @return
     */
    @Override
    public GetMotionEvaluationVo getMotionEvaluation() {
        MotionEvaluation motionEvaluation = motionEvaluationRepository.getByEntity(new MotionEvaluation());
        if (motionEvaluation == null) {
            return new GetMotionEvaluationVo();
        }
        GetMotionEvaluationVo vo = new GetMotionEvaluationVo();
        BeanUtils.copyProperties(motionEvaluation, vo);

        if (motionEvaluation.getEndurance() != null) {
            vo.setEndurance(JSONArray.parseArray(motionEvaluation.getEndurance(), MotionEvaluationJsonParam.class));
        }
        if (motionEvaluation.getStrength() != null) {
            vo.setStrength(JSONArray.parseArray(motionEvaluation.getStrength(), MotionEvaluationJsonParam.class));
        }
        if (motionEvaluation.getSpeed() != null) {
            vo.setSpeed(JSONArray.parseArray(motionEvaluation.getSpeed(), MotionEvaluationJsonParam.class));
        }
        if (motionEvaluation.getAgility() != null) {
            vo.setAgility(JSONArray.parseArray(motionEvaluation.getAgility(), MotionEvaluationJsonParam.class));
        }
        if (motionEvaluation.getFlexibility() != null) {
            vo.setFlexibility(JSONArray.parseArray(motionEvaluation.getFlexibility(), MotionEvaluationJsonParam.class));
        }

        return vo;
    }

    /**
     * 获取运动评估参数 列表
     *
     * @param param
     * @return
     */
    @Override
    public PageResult<GetMotionEvaluationListVo> getMotionEvaluationList(GetMotionEvaluationListParam param) {

        SelectMotionEvaluationDto dto = new SelectMotionEvaluationDto();
        BeanUtils.copyProperties(param, dto);
        Page<MotionEvaluation> page = motionEvaluationRepository.selectMotionEvaluation(dto);
        List<MotionEvaluation> motionEvaluationList = page.getRecords();
        List<GetMotionEvaluationListVo> list = new ArrayList<>();
        for (MotionEvaluation motionEvaluation : motionEvaluationList) {
            GetMotionEvaluationListVo vo = new GetMotionEvaluationListVo();
            BeanUtils.copyProperties(motionEvaluation, vo);

            list.add(vo);
        }
        return new PageResult<>(list, page);

    }

    /**
     * 获取运动评估参数下拉列表
     *
     * @param param
     * @return
     */
    @Override
    public List<GetMotionEvaluationDownListVo> getMotionEvaluationDownList(GetMotionEvaluationDownListParam param) {
        MotionEvaluation po = new MotionEvaluation();
        if (param != null) {
            BeanUtils.copyProperties(param, po);
        }
        List<MotionEvaluation> motionEvaluationList = motionEvaluationRepository.getListByEntity(po);

        List<GetMotionEvaluationDownListVo> list = new ArrayList<>();
        for (MotionEvaluation motionEvaluation : motionEvaluationList) {
            GetMotionEvaluationDownListVo vo = new GetMotionEvaluationDownListVo();
            BeanUtils.copyProperties(motionEvaluation, vo);
            list.add(vo);
        }
        return list;
    }


}



